<?php

	include("../includes/connect_db.php");

	extract($_POST, EXTR_SKIP);

	$fullSerial = array();

	switch ($act) {
		case 'weekview':
			$sql = "SELECT WEEK(checkout) AS week, COUNT(checkout) AS amount FROM front_office WHERE idroom = '".$idroom."' AND CONCAT(YEAR(checkout),'-',MONTH(checkout)) = '".$date."' GROUP BY week ORDER BY week ASC";
			$query = mysql_query($sql);
			$fullSerial = array(
								array('period' => 1, 'amount' => 0),
								array('period' => 2, 'amount' => 0),
								array('period' => 3, 'amount' => 0),
								array('period' => 4, 'amount' => 0)
							);
			while ($data = mysql_fetch_array($query)) {
				switch ($data["week"]%4) {
					case 1:
						$dataSerial["period"] = "1st week";
						break;
					case 2:
						$dataSerial["period"] = "2nd week";
						break;
					case 3:
						$dataSerial["period"] = "3rd week";
						break;
					case 0:
						$dataSerial["period"] = "4th week";
						break;
				}
				// $dataSerial["period"] = ($data["week"]%4 == 0) ? 4 : $data["week"]%4;
				$period = ($data["week"]%4 == 0) ? 3 : (($data["week"]%4)-1);
				$fullSerial[$period]["amount"] = $data["amount"];
				// $dataSerial["amount"] = $data["amount"];
				// array_push($fullSerial, $dataSerial);
				// print_r($fullSerial);
			}
			break;
		case 'monthview':
			$sql = "SELECT MONTH(checkout) AS month, COUNT(checkout) AS amount FROM front_office WHERE idroom = '".$idroom."' AND YEAR(checkout) = '".$date."' GROUP BY month ORDER BY month ASC";
			// echo $sql;
			$query = mysql_query($sql);
			$fullSerial = array(
								array('period' => 'January', 'amount' => 0),
								array('period' => 'February', 'amount' => 0),
								array('period' => 'March', 'amount' => 0),
								array('period' => 'April', 'amount' => 0),
								array('period' => 'May', 'amount' => 0),
								array('period' => 'June', 'amount' => 0),
								array('period' => 'July', 'amount' => 0),
								array('period' => 'August', 'amount' => 0),
								array('period' => 'September', 'amount' => 0),
								array('period' => 'October', 'amount' => 0),
								array('period' => 'November', 'amount' => 0),
								array('period' => 'December', 'amount' => 0)
							);
			while ($data = mysql_fetch_array($query)) {
				// $dataSerial["period"] = $data["month"];
				$period = $data["month"]-1;
				$fullSerial[$period]["amount"] = $data["amount"];
				// $dataSerial["amount"] = $data["amount"];
				// array_push($fullSerial, $dataSerial);
			}
			break;
		case 'yearview':
			$sql = "SELECT YEAR(checkout) AS year, COUNT(checkout) AS amount FROM front_office WHERE idroom = '".$idroom."' GROUP BY year ORDER BY year ASC";
			$query = mysql_query($sql);
			while ($data = mysql_fetch_array($query)) {
				$dataSerial["period"] = $data["year"];
				$dataSerial["amount"] = $data["amount"];
				array_push($fullSerial, $dataSerial);
			}
			break;
	}
	echo json_encode($fullSerial);

?>